package com.BlogWebAutoTest.common;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

/**
 * @author krystal
 * @date 2023/3/2 20:52
 */
public class AutotestUtils {
    public static ChromeDriver driver;

    //创建驱动对象
    public static ChromeDriver createDriver(){
        //驱动创建好了/没有创建
        if (driver==null){
            driver=new ChromeDriver();
            //隐式等待
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        }
        return driver;
    }

    @Test
    public List<String> getTime(){
        //文件按天的维度按文件夹保存
        SimpleDateFormat sim1=new SimpleDateFormat("yyyyMMdd-HHmmssSS");
        SimpleDateFormat sim2=new SimpleDateFormat("yyyyMMdd");
        String filename=sim1.format(System.currentTimeMillis());
        String dirname=sim2.format(System.currentTimeMillis());
        List<String> list=new ArrayList<>();
        list.add(dirname);
        list.add(filename);
        return list;
    }

    //获取屏幕截图，把所有的用例执行的结果保存下来
    public void getScreenShor(String str) throws IOException {
        List<String> list=getTime();
        String filename="./src/test/java/com/BlogWebAutoTest/"+list.get(0)+"/"+str+"_"+list.get(1)+".jpg";
        File srcfile=driver.getScreenshotAs(OutputType.FILE);
        //把屏幕截图生成的文件放到指定的路径
        FileUtils.copyFile(srcfile,new File(filename));
    }
}
